/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.dao.UserDAO;
import model.entity.PhongTro;
import model.entity.User;
import model.service.UsersDAOService;

/**
 *
 * @author duy
 */
public class UserDAO implements UsersDAOService {

    private static UserDAO userDAO;

    public static UserDAO getInstance() {
        if (userDAO == null) {
            userDAO = new UserDAO();

        }
        return userDAO;
    }

    @Override
    public List<User> getAllUser() {
        List<User> list = new ArrayList<>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_user";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                User item = new User();
                item.setMauser(rs.getInt("mauser"));
                item.setTendangnhap(rs.getString("tendangnhap"));
                item.setMatkhau(rs.getString("matkhau"));
                item.setHoten(rs.getString("hoten"));
                item.setDiachi(rs.getString("diachi"));
                item.setSdt(rs.getString("sdt"));
                item.setEmail(rs.getString("email"));
                list.add(item);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean checkLogin(String username, String password) {
        boolean isCheck = false;
        try {
            Connection connect = ConnectionFactory.getConnection();
            String sql = "select * from tbl_user where tendangnhap=? and matkhau=? ";
            PreparedStatement pstmt = connect.prepareCall(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs != null) {
                isCheck = rs.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public User getusersbyID(int mauser) {
        User item = new User();

        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_user where mauser=? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, mauser);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMauser(rs.getInt("mauser"));
                item.setTendangnhap(rs.getString("tendangnhap"));
                item.setMatkhau(rs.getString("matkhau"));
                item.setHoten(rs.getString("hoten"));
                item.setDiachi(rs.getString("diachi"));
                item.setSdt(rs.getString("sdt"));
                item.setEmail(rs.getString("email"));

            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return item;
    }

    @Override
    public User getusersbyUserName(String tendangnhap) {
        User item = new User();

        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_user where tendangnhap=? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, tendangnhap);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMauser(rs.getInt("mauser"));
                item.setTendangnhap(rs.getString("tendangnhap"));
                item.setMatkhau(rs.getString("matkhau"));
                item.setHoten(rs.getString("hoten"));
                item.setDiachi(rs.getString("diachi"));
                item.setSdt(rs.getString("sdt"));
                item.setEmail(rs.getString("email"));

            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return item;
    }

}
